// pages/hhh/hhh.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    obj2:[],
    // img:"",
    chat:"",
    name:"",
    nowtime:"",
    chatlist:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.getChat();
  },

  getTime(){
	  var now = new Date();
	  var year = now.getFullYear(); //得到年份
	  var month = now.getMonth();//得到月份
	  var date = now.getDate();//得到日期
	  var day = now.getDay();//得到周几
	  var hour = now.getHours();//得到小时
	  var minu = now.getMinutes();//得到分钟
	  var sec = now.getSeconds();//得到秒
	  month = month + 1;
	  if (month < 10) month = "0" + month;
	  if (date < 10) date = "0" + date;
	  if (hour < 10) hour = "0" + hour;
	  if (minu < 10) minu = "0" + minu;
	  if (sec < 10) sec = "0" + sec;
	  var time = "";
	  //精确到天
		time = year + "-" + month + "-" + date+ " " + hour + ":" + minu + ":" + sec;
    // return time;
    console.log(time)
    return time
},
getChatt(e){
  console.log(e.detail.value)
  this.setData({
    chat:e.detail.value
  });
},
//获取评论
getChat(){
  
  wx.showLoading({
    title: '请稍后...',
  })
  wx.request({ 
    url: 'http://127.0.0.1:5000/getAllChat', 
    data: {
      
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success:(res)=> {
      console.log(res.data.list)
      if(res.data.code==200){
        this.setData({
          // img:wx.getStorageSync('userInfo').avatarUrl,
         obj2:res.data.list
        });
       
        wx.hideLoading()
      }else{
       
      }
    }
  })
},
getChatt(e){
  console.log(e.detail.value)
  this.setData({
    chat:e.detail.value
  });
},
 // 提交
 addBtn(){
    wx.showLoading({
      title: '请稍后...',
    })
    wx.request({ 
      url: 'http://127.0.0.1:5000/addChat', 
      method:"POST",
      data: {
        book_id:this.data.book_id,
        title:this.data.title,
        chat:this.data.chat,
        tel:wx.getStorageSync('tel'),
        img:wx.getStorageSync('info').img,
        xingming:wx.getStorageSync('info').xingming,
        shijian:this.getTime()
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success:(res)=> {
    
        console.log(res.data)
        wx.hideLoading()
        if(res.data.code==200){
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: "发布成功！",
            showCancel:false,
            success:()=> {
              // wx.navigateBack({
              //   delta:1
              // })
              this.setData({
                chat:"",
              }); 
            }
          })
        }else{
          wx.showModal({
            title: '提示',
            content: "发布失败！",
            showCancel:false,
            success () {
              
            }
          })
        }
      }
    })
  },
  
})